
/**
 * 初始化消息组件
 */
function initLoading(Vue) {
  Vue.prototype.$vcui.loadon = (timeout = 10000) => {
    Vue.prototype.$vcevent.emit('loading', true)
    setTimeout(() => { Vue.prototype.$vcui.loadoff() }, timeout)
  }
  Vue.prototype.$vcui.loadoff = () => {
    Vue.prototype.$vcevent.emit('loading', false)
  }
}

/**
 * 初始化消息组件
 */
function initNotice(Vue) {

  let _emit = (options) => Vue.prototype.$vcevent.emit('noticed', options)

  let _notice = (color, msg, duration = -1, cb) => {
    _emit({
      flag: true,
      message: msg,
      timeout: duration,
      color: color,
    })
    setTimeout(() => {
      _emit({ flag: false, })
      cb && cb()
    }, duration)
  }
  Vue.prototype.$vcui.notice = (msg, duration, cb) => _notice('primary', msg, duration, cb)
  Vue.prototype.$vcui.success = (msg, duration, cb) => _notice('success', msg, duration, cb)
  Vue.prototype.$vcui.error = (msg, duration, cb) => _notice('error', msg, duration, cb)
  Vue.prototype.$vcui.warn = (msg, duration, cb) => _notice('orange', msg, duration, cb)
}

export default {
  install(Vue) {
    Vue.prototype.$vcui = Vue.prototype.$vcui || {}
    initLoading(Vue)
    initNotice(Vue)
  }
}
